﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utility.NodeGraphs;
using System.Reflection;

namespace Utility.BehaviourTree
{
	public class BTSetting_Node : TreeNode
	{
		public enum NodeTypes
		{
			Decorator,
			Composite,
			Leaf,
		}

		public NodeTypes nodeType;
		public string typeName;

		public string nodeName;

		public string[] parameters = new string[0];

		public override int ChildPortCount_Min
		{
			get
			{
				switch (nodeType)
				{
					case NodeTypes.Composite:
					case NodeTypes.Decorator:
						return 1;
					case NodeTypes.Leaf:
					default:
						return 0;
				}
			}
		}

		public override int ChildPortCount_Max
		{
			get
			{
				switch (nodeType)
				{
					case NodeTypes.Composite:
						return -1;
					case NodeTypes.Decorator:
						return 1;
					case NodeTypes.Leaf:
					default:
						return 0;
				}
			}
		}

		public object[] GetArgsArray ()
		{
			ParameterInfo[] paramInfos;
			if (!BTSetting_NodeGraph.TryGetTypeParams (typeName, out paramInfos))
			{
				return null;
			}

			var args = new object[parameters.Length];
			for (int i = 0; i < parameters.Length; i++)
			{
				object arg = null;
				string str = parameters[i];

				var type = paramInfos[i].ParameterType;
				if (type == typeof (int))
				{
					int val = int.TryParse (str, out val) ? val : default;
					arg = val;
				}
				else if (type == typeof (float))
				{
					float val = float.TryParse (str, out val) ? val : default;
					arg = val;
				}
				else if (type == typeof (bool))
				{
					bool val = bool.TryParse (str, out val) ? val : default;
					arg = val;
				}
				else if (type == typeof (string))
				{
					arg = str;
				}

				args[i] = arg;
			}
			return args;
		}
	}
}